package cn.lili.controller.wxpay;


import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

/**
 * 数据加密/解密工具
 *
 * @author MSI
 */
public class AesEncryptUtils {
    // 算法名称
    public static final String ALGORITHM = "AES/ECB/PKCS5Padding";
    public static final String DEFAULT_KEY = "qzMgcxler7ONNwYx";
    private static final Logger log = LoggerFactory.getLogger(AesEncryptUtils.class);
    private static final String CHARSET_NAME = "UTF-8";
    private static final String AES_NAME = "AES";

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    /**
     * 加密
     *
     * @param content
     * @param key
     *
     * @return
     */
    public static String encrypt(String content, String key) {
        byte[] result = null;
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARSET_NAME), AES_NAME);
//            AlgorithmParameterSpec paramSpec = new IvParameterSpec(new byte[16]);
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            result = cipher.doFinal(content.getBytes(CHARSET_NAME));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return Base64.encodeBase64String(result);
    }

    public static String encrypt(String content) {
        return encrypt(content, DEFAULT_KEY.replace("|", ""));
    }

    /**
     * 解密
     *
     * @param content 待解密内容
     * @param key     解密密钥
     *
     * @return
     */
    public static String decrypt(String content, String key) {
        if (StringUtils.isEmpty(content)) {
            return content;
        }
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARSET_NAME), AES_NAME);
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            return new String(cipher.doFinal(Base64.decodeBase64(content)), CHARSET_NAME);
        } catch (IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException | NoSuchAlgorithmException | UnsupportedEncodingException | InvalidKeyException ex) {
            log.error("AES解密失败：{}", ex.getMessage());
        }
        return StringUtils.EMPTY;
    }

    public static String decrypt(String content) {
        return decrypt(content, DEFAULT_KEY.replace("|", ""));
    }

//    public static void main(String[] args) throws Exception {
////        String content = decrypt("U9Bvp+to4C/x6Ec3kWCRn5/W8TbQBgoHVkbkAeDeiCipRp2MUj0QsL6GEKsutT9vfZtONHL07bs9d2wvdW1g/kX/INMzCbumDKZH32dr024=", "oebdqh0189416010");
//
//        //String content = decrypt("sQsFhYdpn4bw6/xI6O04Mp4PlqciqX0PoQox8XcH8ytP7pwrdHZ6fk0Dr79XgctO0tTQ5wmZ776Yduhwpl+VoaGItw72e9OSULJsNZh2lm0uVeklxSlTGvQKwiDjUasek+7cvhIBjwZbLmJdnMrqooMwflge7nqG9HNBW3gvZfs=", DEFAULT_KEY.replace("|", ""));
//
////        System.out.println(content);
//
//        String content1=encrypt("2KO4E6OTDZYCRPJN");
//        String content2=decrypt(content1);
//        System.out.println(content1);
//        System.out.println(content2);
//    }

}
